package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataByDayDto;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 条件分页查询设备数据
     *
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {

        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        //前端传递的时间是一个long类型的毫秒值，作为条件查询数据库需要转换为日期类型
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();
        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));
        //查询
        Page<DeviceDataVo> page = ( Page<DeviceDataVo>)deviceDataMapper.selectByList(deviceDataPageReqDto);

        return PageResponse.of(page, DeviceDataVo.class);
    }

    @Override
    public List<DeviceDataGraphVo> getDeviceDateListByDay(DeviceDataByDayDto dto) {
        dto.setStartDate(LocalDateTimeUtil.of(dto.getStartTime()));
        dto.setEndDate(LocalDateTimeUtil.of(dto.getEndTime()));
        List<DeviceDataGraphVo>  vos= deviceDataMapper.getDeviceDateListByDay(dto);

        Map<String,Double> map =vos.stream().collect(Collectors.toMap(
                DeviceDataGraphVo::getDateTime,
                DeviceDataGraphVo::getDataValue
        ));
        for (int i=0;i<24;i++){
           String time = String.format("%02d",i)+":00";
           if (!map.containsKey(time)){
               DeviceDataGraphVo deviceDataGraphVo = new DeviceDataGraphVo();
               deviceDataGraphVo.setDateTime(time);
               deviceDataGraphVo.setDataValue(0.0);
               vos.add(i,deviceDataGraphVo);
           }
        }
        return vos;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(DeviceDataByDayDto dto) {
        dto.setStartDate(LocalDateTimeUtil.of(dto.getStartTime()));
        dto.setEndDate(LocalDateTimeUtil.of(dto.getEndTime()));
        List<DeviceDataGraphVo>  vos= deviceDataMapper.queryDeviceDataListByWeek(dto);

        Map<String ,Double> map =vos.stream().collect(Collectors.toMap(
                DeviceDataGraphVo::getDateTime,
                DeviceDataGraphVo::getDataValue
        ));
        for (int i=0;i<7;i++){
            String time = LocalDateTimeUtil.format(LocalDateTimeUtil.of(dto.getStartDate()).plusDays(i), "MM月dd日");
            if (!map.containsKey(time)) {
                DeviceDataGraphVo deviceDataGraphVo = new DeviceDataGraphVo();
                deviceDataGraphVo.setDateTime(time);
                deviceDataGraphVo.setDataValue(0.0);
                vos.add(i,deviceDataGraphVo);
            }
        }
        return vos;
    }

}